#!/usr/bin/python


# for now this script assumes all servers are up on the correct ports!

import ConfigParser 
import os
import sys
import filecmp
from shutil import copyfile

client_command = ['s', 'r']
client_mode = ['a','p']
client_folder = "./client/"
server_folder = "./server/"
filename = "something.h"
log_folder = "./client_out/"
log_compare_folder = "./client_compare/"
diff_folder = "./client_diff/"
diff2_folder = "./client_diff2/"
server_port = "1234"

if len(sys.argv) < 3:
	print 'Usage : run_client_servers.py <config_file> <server_name>'
	sys.exit(1)

config = sys.argv[1] 
server_name = sys.argv[2]

# read from log file the servers and their ports
parser = ConfigParser.ConfigParser()
parser.read(config)
section_s = "servers"
options = parser.options(section_s)
servers_dict = {}
for op in options :
	servers_dict[op] = parser.get(section_s, op)

section_c = "clients"
options = parser.options(section_c)
clients = []
for op in options:
	clients.append(op)


section_l = "logs"
options = parser.options(section_l)
compare_logs = {} 
for op in options :
	compare_logs[op] = parser.get(section_l,op)


section_f = "files"
options = parser.options(section_f)
files = []
for op in options :
	files.append(op)


log_files = []

spath =  files[1] # store - what we want to copy to the server
rpath = server_folder + files[0] #ret - what we want to retrieve from the server


#go over all the servers and run all the commands and create log files
server =server_name 
#server_port = servers_dict[k]
for client_name in clients:
	for cmd in client_command :
		for mode in client_mode:
			f = files[0]
			if mode == 's':
				f = files[0]	
			else :
				f = files[1]
			str_i = str(mode) + str(cmd) 
			log = log_folder + client_name + "_" + server + "_" + str_i
			out = log_folder + client_name + "_" + server + "_" + str_i
			log_files.append(log)
			run_client_cmd = client_folder   + client_name + " 127.0.0.1:" + server_port + " " + cmd +" " + \
			mode + " " + f + " " + log + ".log > " + log + ".out" 
			print run_client_cmd
			os.system(run_client_cmd)
				

# compare the logs to the correct ones

for log in log_files:
	print " log : " + log
	logg = str(log[-2]) + str(log[-1])
	print "log : " + logg
	file = parser.get(section_l, logg)
	cmd2 = "./compare_files " + log +".log " + log_compare_folder + file +" > " + diff2_folder + log.split('/')[-1] + "-" + file  	
	cmd = "diff "+  log +".log " + log_compare_folder + file +" > " + diff_folder + log.split('/')[-1] + "-" + file  
	print cmd
	outres = os.system(cmd)
	if (outres != 0) : 
		# files are not the same	
		print "ERROR1 : the log file : " + log + "  is not the same as :" +  log_compare_folder + file
	print cmd2
	outres = os.system(cmd2)
	if (outres != 0) : 
		# files are not the same	
		print "ERROR2 : the log file : " + log + "  is not the same as :" +  log_compare_folder + file
	


#compare the actual files transmitted


rnew =  files[0]
snew = server_folder + files[1] #waht we stored in the server

print "FILE CMP : " + str(filecmp.cmp(rpath, rnew)) + " for : " + rpath + "," + rnew
if filecmp.cmp(rpath, rnew) != True :
	print "FILE : " + rpath + " and " +  rnew + "ARE NOT THE SAME"
print "FILE CMP : " + str(filecmp.cmp(spath, snew)) + " for : " + spath + "," + snew
if filecmp.cmp(spath, snew) != True:
	print "FILE : " + spath + " and " +  snew + "ARE NOT THE SAME"



# check connection to non-existant server 

client_name = clients[0]

for cmd in client_command :
	for mode in client_mode:
		log = log_folder + "bad_server" +  str(mode) + str(cmd)
		run_client_cmd = client_folder   + client_name + " 127.0.0.1:" +"0009" + " " + cmd +" " + mode + " " + files[0] + " " + log + ".log > " + log + ".out"


# check bad file logs: 


for cmd in client_command :
	for mode in client_mode:
		log = log_folder + "bad_file" + str(mode) + str(cmd)
		run_client_cmd = client_folder   + client_name + " 127.0.0.1:" + server_port + " " + cmd +" " + mode + " " + "somebadfile" + " " + log + ".log > " + log + ".out"





		


